# 准确率: 98%
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

# 1 获取数据
iris = load_iris()
# 2 划分数据集
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=6)
# 3 特征工程: 标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4 KNN算法预估器
estimator = KNeighborsClassifier()
# 网格搜索与交叉验证
param_dict = {'n_neighbors': [3,5,7]}
estimator = GridSearchCV(estimator,param_grid=param_dict,cv=4)
estimator.fit(x_train,y_train)
# 5 模型评估
print('最佳参数: ',estimator.best_params_)
print('最佳结果: ',estimator.best_score_)
print('最佳估计器: ',estimator.best_estimator_)
print('交叉验证结果: ',estimator.cv_results_)


